package com.enums;

import com.config.CommonConstant;
import lombok.Getter;

@Getter
public enum ServiceExceptionEnum {
    // 成功
    OK("00000","Success"),

    // E00 未知的通用错误
    UNKNOWN_FAILURE("E00001","Unknown Failure"),
    OTHER_ABNORMAL("E00002","Other Abnormal"),

    // E01 系统错误
    REQUEST_METHOD_ERR("E01001","请求方式无效"),
    REQUEST_RATE_ERR("E01002","请求次数太频繁了"),
    RAW_DATA_NON_JSON_ERR("E01003","请求参数数据结构错误，Raw数据非Json结构"),
    REQUEST_CONTEXT_HOLDER_NULL("E01004","请求内容RequestContextHolder，获取失败"),
    REQUEST_METHOD_NOT_SUPPORTED_EXCEPTION("E01005","Http请求方式异常"),
    INDEX_OUT_OF_BOUNDS_EXCEPTION("E01006","索引越界异常"),
    ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION("E01007","下标越界异常"),
    NULL_POINT_EXCEPTION("E01008","空指针异常"),
    UN_DEVELOP_ERROR("E01009","未开发错误"),

    // E02 SQL 执行错误
    SQL_EXCEPTION("E02001","SQL 执行异常"),
    SQL_INSERT_FAILURE("E02002","数据表["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"],SQL insert执行失败"),
    SQL_UPDATE_FAILURE("E02003","数据表["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"],SQL update执行失败"),
    SQL_DELETE_FAILURE("E02004","数据表["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"],SQL delete执行失败"),
    SQL_TRANSACTION_FAILURE("E02005","SQL 事务执行失败"),
    SQL_UPDATE_DELETE_DATA("E02006","SQL 更新了已删除数据"),

    // E03 授权错误
    AUTH_INVALID("E03001","授权Key无效"),
    AUTH_EXPIRED("E03002","授权已到期"),
    AUTH_NULL("E03003","缺少授权Key"),
    AUTH_CANCEL("E03004","授权已取消"),
    SIGN_NULL("E03005","缺少签名"),
    SIGN_UTC_NULL("E03006","缺少签名时间戳"),
    TOKEN_NULL("E03007","缺少TOKEN"),
    SIGN_INVALID("E03008","签名无效"),
    SIGN_UTC_EXPIRE("E03009","签名时间错已过期"),

    // E10 运维登录异常
    COMPANY_CODE_INVALID("E10001","企业码["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]无效"),
    COMPANY_STATUS_LOCKED("E10002","企业["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]已被锁定，请联系挂你另有"),
    USER_ACCOUNT_INVALID("E10003","用户帐号无效"),
    USER_PASSWORD_INVALID("E10004","用户密码无效"),
    TOKEN_INVALID("E10009","TOKEN无效"),
    TOKEN_EXPIRE("E10010","TOKEN已过期，请重新登录"),
    USER_STATUS_LOCKED("E10011","用户已被锁定，请联系管理员"),
    // E11 任务稽核异常
    SEARCH_TASK_STATUS_UNABLE_AUDIT("E11001","任务状态["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]不可稽核"),
    SEARCH_TASK_OR_RESULT_NON_EXIST("E11002","查询任务或结果不存在"),
    // E12 运维交互异常
    SHIP_UNIQUE_INFO_REPEAT("E12001","船舶信息核心数据重复"),
    TASK_INFO_SAVE_FAILURE("E12002","航程任务信息保存失败"),
    SEARCH_TASK_STATUS_INIT_ERR("E12003","查询任务尚未启动，无后续数据"),
    SEARCH_TASK_NON_RESULT_AUDIT("E12004", "查询任务尚无结果数据进行稽核"),


    // E41 对外数据请求响应response报错
    RESPONSE_STATUS_CODE_NULL("E41001","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应状态码status_code为null"),
    RESPONSE_STATUS_CODE_NON_SUCCESS("E41002","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应状态码status_code["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1+"]"),
    RESPONSE_DATA_NULL("E41003","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应数据为null"),
    RESPONSE_UNDEFINED_ABNORMAL_EXP("E41004","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应出现未定义异常，异常信息："+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1),
    RESPONSE_TRACK17_RETURN_CODE_FAILURE("E41005","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应的返回失败码code["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1+"]"),
    RESPONSE_DATA_CONVERT_JSON_ERR("E41006","数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求响应的返回数据data转Json失败"),
    RESOURCE_REQUEST_USABLE_TIMES_EMPTY("E41007", "数据["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0+"]请求可用次数已用完"),

    // E42 推送数据报错
    PUSH_DATA_FAILURE("E42001","企业应用["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]，推送数据失败"),
    PUSH_DATA_RESPONSE_STATUS_ERR("E42002","企业应用["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]，推送返回响应码["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1+"]错误"),

    // E50 API接口通用错误
    PARAM_LENGTH_ERR("E50002","参数["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]长度错误，参考标准["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1 +"]"),
    PARAM_FORMAT_ERR("E50003","参数["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]格式错误，要求数据格式为["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1 +"]"),
    PARAM_NULL_ERR("E50004","参数["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]为必填参数，不能为空"),
    PARAM_INVALID_ERR("E50005","参数["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]无效"),
    RESET_CANCEL_TASK_ERR("E50006","已取消任务不可重置"),
    REPEAT_CANCEL_TASK_ERR("E50007","已取消的任务不可重复取消"),
    CANCEL_FINISH_TASK_ERR("E50008","已完成的查询任务不可取消"),
    REFRESH_FINISHED_SEARCH_TASK_ERR("E50009","已完成的查询任务不可刷新执行"),
    REFRESH_CANCEL_SEARCH_TASK_ERR("E50010","已取消的查询任务不可以刷新执行"),
    PARAM_VALUE_OUTSIDE_RANGE_ERR("E50011","参数["+ CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 + "]的值，超出要求的范围["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1+"]"),
    PARAM_VALUE_REPEAT_ERR("E50012","参数["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0 +"]值不可重复，出现重复值["+CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1+"]"),
    OUTSIDE_RESOURCE_RESPONSE_ERR("E50013","调用外部接口返回500处理"),
    SEARCH_TASK_CANCELED_ERR("E50014","查询任务已取消，无法获取轨迹"),
    BOOTED_SEARCH_TASK_NON_RESULT_ERR("E50015","查询任务已启动，但未查询结果记录"),
    INI_TASK_FAILURE("E50016", "初始化任务失败"),


    ;

    /**
     * 错误码
     */
    final String code;
    /**
     * 返回信息
     */
    final String msg;

    ServiceExceptionEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getFormattedMsg(String[] params) {
        if(params == null || params.length == 0){
            return this.msg;
        }else if(params.length == 1){
            if(params[0] == null) params[0] = "";
            return this.msg.replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0,params[0]);
        }else{
            if(params[0] == null) params[0] = "";
            if(params[1] == null) params[1] = "";
            return this.msg.replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0,params[0]).replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1,params[1]);
        }
    }

    public String getFormattedMsg(Integer[] params) {
        if(params == null || params.length == 0){
            return this.msg;
        }else if(params.length == 1){
            String params_0;
            if(params[0] == null) {
                params_0 = "";
            }else {
                params_0 = String.valueOf(params[0]);
            }
            return this.msg.replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0,params_0);
        }else{
            String params_0;
            if(params[0] == null) {
                params_0 = "";
            }else {
                params_0 = String.valueOf(params[0]);
            }
            String params_1;
            if(params[1] == null) {
                params_1 = "";
            }else {
                params_1 = String.valueOf(params[1]);
            }
            return this.msg.replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_0,params_0).replaceAll(CommonConstant.SERVICE_ERROR_REPLACE_CHAR_1,params_1);
        }
    }
}
